<template>
  <header>
    <div class="header-container">
      <div class="logo-container">
        <router-link to="/" class="title">研究生奖学金评选系统</router-link>
      </div>
      <div class="search-container">
        <input type="text" placeholder="搜索一下" class="search-input"/>
        <button class="search-button">搜索</button>
      </div>
      <div class="nav-icons">
        <router-link to="/ideological" class="nav-icon">思政成绩</router-link>
        <router-link to="/candidates" class="nav-icon">候选查询</router-link>
        <router-link to="/competitions" class="nav-icon">学科竞赛</router-link>
        <router-link to="/services" class="nav-icon">社会服务</router-link>
        <div class="dropdown"
             @mouseenter="showDropdown"
             @mouseleave="hideDropdown"
             @click="toggleDropdown">
          <button class="nav-icon dropdown-toggle">
            {{ displayName }}
            <span class="dropdown-arrow">▼</span>
          </button>
          <div class="dropdown-content" :class="{ 'show': isDropdownOpen }">
            <template v-if="isLoggedIn">
              <router-link to="/profile" class="dropdown-item" @click="isDropdownOpen = false">个人资料</router-link>
              <a href="#" class="dropdown-item" @click.prevent="switchAccount">切换账号</a>
              <a href="#" class="dropdown-item" @click.prevent="logout">退出登录</a>
            </template>
            <template v-else>
              <router-link to="/register" class="dropdown-item" @click="isDropdownOpen = false">注册</router-link>
              <router-link to="/login" class="dropdown-item" @click="isDropdownOpen = false">登录</router-link>
            </template>
          </div>
        </div>
      </div>
    </div>
  </header>
</template>

<script setup>
import { ref, computed } from 'vue';
import { useRouter } from 'vue-router';
import { useStore } from 'vuex';
import { ElMessage } from 'element-plus';

const router = useRouter();
const store = useStore();
const isDropdownOpen = ref(false);

// 计算属性
const isLoggedIn = computed(() => store.getters.isAuthenticated);
const userInfo = computed(() => store.getters.currentUser);
const displayName = computed(() => {
  return isLoggedIn.value
      ? userInfo.value?.username || userInfo.value?.userId || '用户'
      : '个人中心';
});

// 下拉菜单控制
const showDropdown = () => isDropdownOpen.value = true;
const hideDropdown = () => isDropdownOpen.value = false;
const toggleDropdown = () => {
  if (window.innerWidth <= 768) {
    isDropdownOpen.value = !isDropdownOpen.value;
  }
};

// 退出登录
const logout = async () => {
  try {
    await store.dispatch('logout');
    ElMessage.success('已退出登录');
    router.push('/login');
  } catch (error) {
    ElMessage.error('退出登录失败');
    console.error('退出登录错误:', error);
  } finally {
    isDropdownOpen.value = false;
  }
};

// 切换账号
const switchAccount = () => {
  store.dispatch('logout');
  router.push('/login');
  isDropdownOpen.value = false;
};
</script>

<style scoped>
/* 保持原有样式不变 */
.header-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #333;
  padding: 10px 20px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
  position: relative;
  z-index: 1000;
}

.logo-container {
  margin-right: 20px;
}

.title {
  color: white;
  text-decoration: none;
  font-size: 20px;
  font-weight: bold;
  transition: color 0.3s;
}

.title:hover {
  color: #4dabf7;
}

.search-container {
  flex-grow: 1;
  display: flex;
  align-items: center;
  max-width: 450px;
  height: 40px;
}

.search-input {
  padding: 10px 15px;
  width: 100%;
  height: 100%;
  border: none;
  border-radius: 4px 0 0 4px;
  font-size: 16px;
  outline: none;
  background-color: #f8f9fa;
  transition: background-color 0.3s;
  box-sizing: border-box;
}

.search-input:focus {
  background-color: white;
  box-shadow: 0 0 0 2px rgba(77, 171, 247, 0.5);
}

.search-button {
  background-color: #575757;
  color: #fff;
  border: none;
  padding: 0 20px;
  height: 100%;
  border-radius: 0 4px 4px 0;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s;
  box-sizing: border-box;
  white-space: nowrap;
}

.search-button:hover {
  background-color: #0056b3;
}

.nav-icons {
  display: flex;
  gap: 15px;
  align-items: center;
}

.nav-icon {
  color: white;
  text-decoration: none;
  padding: 10px 15px;
  border-radius: 4px;
  transition: all 0.3s;
  display: inline-block;
}

.nav-icon:hover {
  background-color: #575757;
  transform: translateY(-2px);
}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-toggle {
  background: none;
  border: none;
  color: white;
  cursor: pointer;
  padding: 10px 15px;
  font-size: inherit;
  font-family: inherit;
  display: flex;
  align-items: center;
  gap: 5px;
}

.dropdown-arrow {
  font-size: 12px;
  transition: transform 0.3s;
}

.dropdown:hover .dropdown-arrow {
  transform: rotate(180deg);
}

.dropdown-content {
  display: none;
  position: absolute;
  right: 0;
  background-color: #333;
  min-width: 180px;
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
  z-index: 1;
  border-radius: 4px;
  overflow: hidden;
  animation: fadeIn 0.2s ease-out;
}

.dropdown-content.show {
  display: block;
}

.dropdown-item {
  color: white;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
  transition: all 0.3s;
  border-bottom: 1px solid #444;
}

.dropdown-item:last-child {
  border-bottom: none;
}

.dropdown-item:hover {
  background-color: #575757;
  padding-left: 20px;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: scale(0.95);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

@media (max-width: 768px) {
  .header-container {
    flex-direction: column;
    padding: 15px;
  }

  .logo-container {
    margin-right: 0;
    margin-bottom: 15px;
    width: 100%;
    text-align: center;
  }

  .search-container {
    width: 100%;
    margin-bottom: 15px;
  }

  .nav-icons {
    width: 100%;
    justify-content: space-around;
    flex-wrap: wrap;
    gap: 10px;
  }

  .dropdown-content {
    position: static;
    width: 100%;
    box-shadow: none;
    border-radius: 0;
    animation: none;
  }

  .dropdown-toggle {
    width: 100%;
    justify-content: center;
  }
}

.router-link-active {
  background-color: #575757;
}
</style>